﻿using System;
using System.Linq;

namespace AIC_Notification
{
    public class StatisticsReport : FormatedReport
    {
        public string InsertEssayStatistics(RallyOfRallySchoolStatistics RallyOfRallySchoolStats, int Rally, int SchoolNumber, int FontSize, bool AsTable, string Label = "")
        {
            string InnerTableStatistics = "";
            int StatsFontSize = FontSize - 4;

            if (AsTable)
                InnerTableStatistics += "<table style=\"margin-left:auto; margin-right: auto;\">";

            RallySchoolStatistics SchoolStats = RallyOfRallySchoolStats.Get(Rally, SchoolNumber);
            if (SchoolStats != null && SchoolStats.Total != 0)
            {
                InnerTableStatistics += String.Format("<tr style=\"font-size: {0}px;\">", StatsFontSize);
                if (Label != "")
                {
                    int RowSpan = (SchoolStats.NumberOfEssay == 0 ? 1 : SchoolStats.NumberOfEssay) + 1;
                    InnerTableStatistics += String.Format("<td rowspan=\"{0}\" style=\"font-size: {1}px;\"><b>{2}</b></td>", RowSpan, StatsFontSize, Label);
                }
                InnerTableStatistics += String.Format("<th>Essay</th><th>Avg</th><th>Min/Max</th></tr>");

                if (SchoolStats.NumberOfEssay == 0)
                    InnerTableStatistics += String.Format("<tr style=\"font-size: {0}px;\"><td colspan = \"3\">Not Started</td></tr>", StatsFontSize);

                for (int i = 1; i <= SchoolStats.NumberOfEssay; i++)
                {
                    RallyEssayStatistics EssayStats = SchoolStats.EssayStatistics.SingleOrDefault(RallyEssayStatistics => RallyEssayStatistics.EssayNumber == i);
                    if (EssayStats != null)
                    {
                        string EssayName = "";
                        string AverageText = "";

                        if (Rally == 0 && SchoolNumber == 0) // CEF
                        {
                            if (i == 1)
                                EssayName = "CEF";
                            if (i == 2)
                                EssayName = "CER";
                        }
                        else if (Rally == 1 && SchoolNumber == 0) // PS
                            EssayName = String.Format("T{0}", i);
                        else if (Rally == 1 && SchoolNumber == 1) // HAL
                        {
                            if (i == 1)
                                EssayName = "MAJ";
                            if (i == 2)
                                EssayName = "HAL";
                        }
                        else
                            EssayName = String.Format("E{0}", i);

                        if (EssayStats.AverageDraft() > 0)
                            AverageText = String.Format("{0:0.0}", EssayStats.AverageDraft());
                        else
                            AverageText = "N/A";

                        InnerTableStatistics += String.Format("<tr style=\"font-size: {0}px;\"><td>{1}</td><td>{2}</td><td>{3}/{4}</td></tr>", 
                            StatsFontSize, 
                            EssayName,
                            AverageText, 
                            EssayStats.GetMinDraft(), 
                            EssayStats.GetMaxDraft());
                    }
                    else
                        Global.SysUtil.Log("Something reall wrong with statsitics", 2);
                }
            }

            if (AsTable)
                InnerTableStatistics += "</table>";

            return InnerTableStatistics;
        }

        public string InsertCompletionStatistics(RallyOfRallySchoolStatistics RallyOfRallySchoolStats, int Rally, int SchoolNumber, int FontSize, bool AsTable, string Label = "")
        {
            string InnerTableStatistics = "";
            int StatsFontSize = FontSize - 4;

            if (AsTable)
                InnerTableStatistics += "<table style=\"margin-left:auto; margin-right: auto;\">";

            RallySchoolStatistics Stats = RallyOfRallySchoolStats.Get(Rally, SchoolNumber);
            if (Stats != null && Stats.Total != 0)
            {
                bool ShowRallyStatus = (Stats.Rally == RallyHelper.CurrentRally) && (Stats.StatusRed > 0 || Stats.StatusYellow > 0 || Stats.StatusGreen > 0);

                if (ShowRallyStatus)
                {
                    InnerTableStatistics += String.Format("<tr style=\"font-size: {0}px;\">", StatsFontSize);

                    if (Label != "")
                    {
                        int RowSpan = 1;
                        InnerTableStatistics += String.Format("<td rowspan=\"{0}\" style=\"font-size: {1}px;\"><b>{2}</b></td>", RowSpan, StatsFontSize, Label);
                    }

                    InnerTableStatistics += String.Format("<td style=\"background-color: #F8D3D3;\">{0:0.0}%</td>", ((double)Stats.StatusRed / (double)Stats.Total) * 100);
                    InnerTableStatistics += String.Format("<td style=\"background-color: #FFFFCC;\">{0:0.0}%</td>", ((double)Stats.StatusYellow / (double)Stats.Total) * 100);
                    InnerTableStatistics += String.Format("<td style=\"background-color: #CCFFCC;\">{0:0.0}%</td>", ((double)Stats.StatusGreen / (double)Stats.Total) * 100);
                    InnerTableStatistics += "</tr>";
                }
                else
                {
                    InnerTableStatistics += String.Format("<tr style=\"font-size: {0}px;\">", StatsFontSize);

                    if (Label != "")
                    {
                        int RowSpan = 2;
                        InnerTableStatistics += String.Format("<td rowspan=\"{0}\" style=\"font-size: {1}px;\"><b>{2}</b></td>", RowSpan, StatsFontSize, Label);
                    }

                    InnerTableStatistics += "<th>NS</th><th>IP</th><th>CP</th>";
                    InnerTableStatistics += "</tr>";

                    InnerTableStatistics += String.Format("<tr style=\"font-size: {0}px;\">", StatsFontSize);
                    InnerTableStatistics += String.Format("<td>{0:0.0}%</td>", ((double)Stats.NotStarted / (double)Stats.Total) * 100);
                    InnerTableStatistics += String.Format("<td>{0:0.0}%</td>", ((double)Stats.InProgress / (double)Stats.Total) * 100);
                    InnerTableStatistics += String.Format("<td>{0:0.0}%</td>", ((double)Stats.Completed / (double)Stats.Total) * 100);
                    InnerTableStatistics += "</tr>";
                }
            }

            if (AsTable)
                InnerTableStatistics += "</table>";

            return InnerTableStatistics;
        }

    }
}
